<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户资金明细表
class Subtasks_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'subtasks';
        $this->complaint_table = 'complaint';
        $this->account_table = 'account';
        $this->finance_list_table = 'finance_list';
    }
    function subtask_list($where = array()){
        $this->db->select("*");
        $this->db->from($this->table);
        $this->db->where($where);
        return $this->db->get()->result_array();
    }
    /**
     * 体检一个任务
     * @param $insert_arr
     * @return mixed
     */
    function add_task($insert_arr){
        //var_dump($insert_arr);exit();
       return $this->db->insert($this->table,$insert_arr);
    }
    function update_subtask($update_data,$where=""){
        if(empty($where)){
            return false;
        }
        return $this->db->where($where)->update($this->table, $update_data);
    }

    function update_complaint($update_data,$where=""){
        if(empty($where)){
            return false;
        }
        return $this->db->where($where)->update($this->complaint_table, $update_data);
    }
    /**
     * 我的子任务信息
     * @param $tid
     * @param $uid
     * @return mixed
     */
    function my_subtask($tid,$uid){
        $sql = "select * from tan_subtasks where uid={$uid} and tid={$tid} and is_end=0";
        $res = $this->db->query($sql)->row_array();
        return $res;
    }

    /**
     * 统计主任务已完成和进行中的数量
     * @param $tid
     */
    function count_subtask($tid){
        $sql = "select count(*) as num from tan_subtasks where tid={$tid} and is_end<2";
        $res = $this->db->query($sql)->row_array();
        return $res['num'];
    }


    function complaint_list($where = array()){
        $this->db->select("*");
        $this->db->from($this->complaint_table);
        $this->db->where($where);
        return $this->db->get()->result_array();
    }
    //发放佣金
    function give_money($sid){
        $subtask = $this->db->query("select st.*, t.commission from tan_subtasks st left join tan_task t on t.tid=st.tid where st.sid=$sid")->row_array();
        if ($subtask['is_gived']==0){
            $account = $this->db->query("select * from tan_account where user_id={$subtask['uid']}")->row_array();
            $amount = $subtask['commission'];

            $update_account = array(
                'balance' => $account['balance'] + $amount,//账户余额
                'total' => $account['total'] + $amount,//总额金额
            );
            $res1 =  $this->db->where(array('user_id'=>$subtask['uid']))->update($this->account_table, $update_account);
            //弹溜溜明细
            $finance_list_data = array();
            $finance_list_data['user_id'] = $subtask['uid'];
            $finance_list_data['content'] = '任务佣金'.$amount.'溜溜币';
            $finance_list_data['finance'] = $amount;
            $finance_list_data['finance_balance'] = $update_account['balance'];
            $finance_list_data['reward_type'] = 'spread_award';
            $finance_list_data['type'] = 1;//收入
            $finance_list_data['create_time'] = time();
            $finance_list_data['month'] = date("Ym");
            $finance_list_data['day'] = date("Ymd");
            $res2 = $this->db->insert($this->finance_list_table,$finance_list_data);
        }else{
            $res1 = $res2 = true;
        }
        return $res1;
    }
}
